package com.yly.doctorapp.ui.prescription;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;

import com.alibaba.fastjson.JSON;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.yly.doctorapp.R;
import com.yly.doctorapp.adapter.ProductionAdapter;
import com.yly.doctorapp.base.BaseActivity;
import com.yly.doctorapp.bean.PrescriptionFirstBean;
import com.yly.doctorapp.bean.ProductionBean;
import com.yly.doctorapp.bean.ResponsDataBean;
import com.yly.doctorapp.constant.Configution;
import com.zhcx.zhcxlibrary.utils.StringUtils;
import com.zhcx.zhcxlibrary.utils.ToastUtils;

import org.xutils.common.Callback;
import org.xutils.common.util.LogUtil;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class SerchActivity extends BaseActivity {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.edit_produce)
    EditText editProduce;
    @BindView(R.id.lv_production)
    ListView lvProduction;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    private List<ProductionBean> mProductionList = new ArrayList<>();
    private ProductionAdapter adapter;
    private ClassicsHeader mClassicsHeader = null;
    private String keyword = "";
    private int pagerNum = 1;

    @Override
    protected int getNaviteColor() {
        return 0;
    }

    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_serch;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        editProduce.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (!StringUtils.isEmpty(s.toString())) {
                    pagerNum = 1;
                    mProductionList.clear();
                    keyword = s.toString();
                    initGetData(s.toString());
                } else {
                    mProductionList.clear();
                    adapter.notifyDataSetChanged();
                }
            }
        });
    }

    @Override
    protected void initData() {
        adapter = new ProductionAdapter(SerchActivity.this, mProductionList);
        lvProduction.setAdapter(adapter);
        lvProduction.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent();
                intent.putExtra("production", mProductionList.get(position));
                setResult(RESULT_OK, intent);
                finish();
            }
        });
        mClassicsHeader = (ClassicsHeader) refreshLayout.getRefreshHeader();
        mClassicsHeader.setLastUpdateTime(new Date(System.currentTimeMillis()));
        mClassicsHeader.setTimeFormat(new SimpleDateFormat("更新于 MM-dd HH:mm"));
        refreshLayout.getLayout().bringChildToFront(lvProduction);
        refreshLayout.setEnableAutoLoadmore(false);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                pagerNum = 1;
                mProductionList.clear();
                if (!StringUtils.isEmpty(keyword)) {
                    initGetData(keyword);
                }
            }
        });
        refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                pagerNum++;
                if (!StringUtils.isEmpty(keyword)) {
                    initGetData(keyword);
                }
            }
        });
    }

    @OnClick(R.id.iv_back)
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
        }
    }

    //拿到产品列表
    public void initGetData(String keyword) {
        RequestParams params = new RequestParams(Configution.HOST + Configution.QUERYPRODUCTIONITEM);
        params.addBodyParameter("keywords", keyword);
        params.addBodyParameter("pageSize", "20");
        params.addBodyParameter("isSingle","0");
        params.addBodyParameter("pageNumber", "" + pagerNum);
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                LogUtil.d(result);
                if (!StringUtils.isEmpty(result)) {
                    ResponsDataBean responsDataBean = JSON.parseObject(result, ResponsDataBean.class);
                    if (responsDataBean.getCode().equals("1000")) {
                        if (!StringUtils.isEmpty(responsDataBean.getData())) {
                            PrescriptionFirstBean prescriptionFirstBean = JSON.parseObject(responsDataBean.getData(), PrescriptionFirstBean.class);
                            if (!StringUtils.isEmpty(prescriptionFirstBean.getList())) {
                                List<ProductionBean> productionBeanList = JSON.parseArray(prescriptionFirstBean.getList(), ProductionBean.class);
                                mProductionList.addAll(productionBeanList);
                            }
                        }
                    }
                    adapter.notifyDataSetChanged();
                }
                if (null != refreshLayout) {
                    refreshLayout.finishRefresh();
                    refreshLayout.finishLoadmore();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                ToastUtils.show(getApplicationContext(), "网络连接失败", 100);
                if (null != refreshLayout) {
                    refreshLayout.finishRefresh();
                    refreshLayout.finishLoadmore();
                }
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
}
